package com.csi.okr.mobile.company.service.impl;

import com.alibaba.fastjson.JSON;
import com.csi.okr.common.util.JsonUtil;
import com.csi.okr.common.util.Pagination;
import com.csi.okr.mobile.common.dao.*;
import com.csi.okr.mobile.common.task.DataTask;
import com.csi.okr.mobile.company.service.CompanyService;
import com.csi.okr.mobile.department.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private VOkrEmplDao emplDao;
    @Resource
    private VOkrEmplOrgDao emplOrgDao;
    @Resource
    private VOkrOrgDao orgDao;
    @Resource
    private VOkrPostDao postDao;
    @Resource
    private VOkrPostSeqDao postSeqDao;
    @Resource
    private TOkrObjectivesDao objDao;
    @Resource
    private DeptService deptService;

    @Override
    public Object getTopOrg() {
        //序列化
        String format = JSON.toJSONString(DataTask.ORG_STRUCTURE);
        //正则替换掉原有的key 为了调用前端组织树的js方法
        format = format.replaceAll("childOrg", "list").replaceAll("orgZhSname", "name").replaceAll("orgCode", "url");
        return format;
    }

    @Override
    public Object getDefaultOrgCode(String memberCode) {
        Map<String, Object> first = emplOrgDao.getUniqueBySql("SELECT * FROM V_OKR_EMPL_ORG WHERE EMPL_CODE = '" + memberCode + "'");
        return first.get("ORG_CODE");
    }

    @Override
    public Object getOrgByOrgCode(String orgCode) {
        Map<String, Object> first = orgDao.getUniqueBySql("SELECT * FROM V_OKR_ORG WHERE ORG_CODE = '" + orgCode + "'");
        return first.get("ORG_ZH_SNAME");
    }

    @Override
    public Object getChildOrg(String orgCode) {
        List<Map<String, Object>> orgList = orgDao.excuteSql("SELECT V2.* FROM V_OKR_ORG V1,V_OKR_ORG V2 WHERE V1.ORG_UUID = V2.ORG_PARENT_UUID AND V1.ORG_CODE = '" + orgCode + "' ORDER BY V2.ORG_LEVEL,V2.ORG_SEQ_NUM");
        return orgList;
    }

    @Override
    public Object getSubsetOrg(String orgCode, String memberCode, String year, String month, String quarter) {
        if (orgCode == null || "".equals(orgCode)) {
            Map<String, Object> first = emplOrgDao.getUniqueBySql("SELECT * FROM V_OKR_EMPL_ORG WHERE EMPL_CODE = '" + memberCode + "'");
            orgCode = (String) first.get("ORG_CODE");
        }

        return getEmpLShow(orgCode, memberCode, year, month, quarter);
    }

    /**
     * 组装数据 - 公司OKR
     *
     * @param orgCode
     * @param emplCode
     * @return
     */
    private Object getEmpLShow(String orgCode, String emplCode, String year, String month, String quarter) {

//		String sql = "SELECT DISTINCT T1.PERSON_LIABLE_NAME,T1.PERSON_LIABLE_CODE,V3.POST_ZH_SNAME,V4.POSTSEQ_NAME,V3.SORT_ID SORT_V3,V4.SORT_ID SORT_V4 FROM T_OKR_OBJECTIVES T1 LEFT JOIN V_OKR_EMPL_ORG V2 ON T1.PERSON_LIABLE_CODE = V2.EMPL_CODE LEFT JOIN V_OKR_POST V3 ON V2.POST_CODE = V3.POST_CODE LEFT JOIN V_OKR_POST_SEQ V4 ON V3.POSTSEQ_UUID = V4.POSTSEQ_UUID LEFT JOIN V_OKR_EMPL V5 ON V5.EMPL_CODE = V2.EMPL_CODE  WHERE V5.EMPL_STATUS = 1 AND T1.OWNER_DEPTCODE = '"+orgCode+"' AND (T1.IS_VISIBLE_OVERALL = 1 OR T1.PERSON_LIABLE_CODE = '"+emplCode+"' )";
//
//		if(month != null && !"".equals(month) && !"0".equals(month)){
//			sql += " AND T1.O_MONTH = '"+month+"'";
//		}
//
//		if (quarter != null && !"".equals(quarter) && !"Q0".equals(quarter)) {
//			sql += " AND T1.O_QUARTER = '"+quarter+"'";
//		}
//
//		if (year != null  && !"".equals(year) && !"请选择".equals(year)) {
//			if(Integer.parseInt(year) != 0){
//				sql += " AND T1.O_YEAR = " + Integer.parseInt(year);
//			}
//		}
//
//		sql += "ORDER BY V3.SORT_ID,V4.SORT_ID ASC";
//
//		List<Map<String, Object>> data = objDao.excuteSql(sql);

        return deptService.getSubsetOrg(orgCode, emplCode, year, month, quarter);
    }

    //根据条件拼接SQL
    public String buildSql(String view, String sql, String year, String month, String quarter) {

        if (month != null && !"".equals(month) && !"0".equals(month)) {
            sql += " AND T1.O_MONTH = '" + month + "'";
        }

        if (quarter != null && !"".equals(quarter) && !"Q0".equals(quarter)) {
            sql += " AND T1.O_QUARTER = '" + quarter + "'";
        }

        if (year != null && !"".equals(year) && !"请选择".equals(year)) {
            if (Integer.parseInt(year) != 0) {
                sql += " AND T1.O_YEAR = " + Integer.parseInt(year);
            }
        }

//		if(view.equals("V_OKR_POST_SEQ")){
//			sql += " ORDER BY V4.SORT_ID ASC ";
//		}else if(view.equals("V_OKR_POST")){
//			sql += " ORDER BY V3.SORT_ID ASC ";
//		}else if(view.equals("V_OKR_EMPL")){
//			sql += " ORDER BY V2.PERSON_SORT_ID ASC ";
//		}

        sql += "ORDER BY V3.SORT_ID,V4.SORT_ID ASC";

        return sql;
    }


    @Override
    public Object getOkrByMemberCode(int pageIndex, int pageSize, String emplCode, String orgCode, String memberCode, String month, String year, String quarter, String state, String odefinition) {

        Map<String, Object> first = emplOrgDao.getUniqueBySql("SELECT * FROM V_OKR_EMPL_ORG WHERE EMPL_CODE = '" + memberCode + "'");
        String org_code = (String) first.get("ORG_CODE");

        if(orgCode.equals(org_code)){
            return deptService.getOkrByMemberCode(pageIndex, pageSize, orgCode, memberCode,emplCode, month,
                     year,  quarter,  state,  odefinition);
        }

//        String sql = "SELECT T1.*, T3.O_SCORE_SUPERIOR FROM T_OKR_OBJECTIVES T1 LEFT JOIN T_OKR_SCORE_DETAIL_OBJECTIVES T3 ON T3.O_UNIQUE_ID_CODE = T1.O_UNIQUE_ID_CODE WHERE T1.OWNER_DEPTCODE = '" + orgCode + "' AND T1.PERSON_LIABLE_CODE = '" + memberCode + "' AND T1.IS_VISIBLE_OVERALL = 1";
        String sql = "SELECT T1.*,T3.O_SCORE_SUPERIOR FROM T_OKR_OBJECTIVES T1 LEFT JOIN T_OKR_DESIGNATED_VISIBLE T2 ON T1.O_UNIQUE_ID_CODE = T2.O_UNIQUE_ID_CODE LEFT JOIN T_OKR_SCORE_DETAIL_OBJECTIVES T3 ON T1.O_UNIQUE_ID_CODE = T3.O_UNIQUE_ID_CODE WHERE T1.PERSON_LIABLE_CODE = '" + emplCode + "' AND T1.OWNER_DEPTCODE = '" + orgCode + "' AND (T1.IS_VISIBLE_OVERALL = 1 OR (T1.IS_VISIBLE_DESIGNATED = 1 AND T2.DESIGNATEDE_PERSON_CODE = '" + memberCode + "'))";

//        if (orgCode != null && !"".equals(orgCode)) {
//            sql += " AND T1.OWNER_DEPTCODE = '" + orgCode + "'";
//        }

        if (month != null && !"".equals(month) && !"0".equals(month)) {
            sql += " AND T1.O_MONTH = '" + month + "'";
        }

        if (quarter != null && !"".equals(quarter) && !"Q0".equals(quarter)) {
            sql += " AND T1.O_QUARTER = '" + quarter + "'";
        }

        if (state != null && !"".equals(state)) {
            //第一次进入
            if (state.equals("query")) {
                //暂无
            } else if (state.equals("oauth")) {
                sql += " AND T1.O_STATE != 'S5'";
            } else if ("0".equals(state)) {
                //暂无
            } else {
                sql += " AND T1.O_STATE = '" + state + "'";
            }
        }

        if (odefinition != null && !"".equals(odefinition)) {
            sql += " AND T1.O_DEFINITION = '" + odefinition + "'";
        }

        if (year != null && !"".equals(year) && !"请选择".equals(year)) {
            if (Integer.parseInt(year) != 0) {
                sql += " AND T1.O_YEAR = " + Integer.parseInt(year);
            }
        }

        sql += " ORDER BY T1.O_YEAR DESC,T1.O_QUARTER DESC,T1.O_MONTH DESC,T1.O_CODE ASC";

        Pagination<Map<String, Object>> data = objDao.findSqlPagination(sql, pageIndex, pageSize);

        return JsonUtil.parseJson(data);
    }

}
